package org.zch.common.utils;


import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * String 工具类
 *
 * @Author 刘新
 * @Date 2024/1/15 11:29
 */
public class StringUtils {

    /**
     * String 转 Map
     * @param input
     * @return
     */
    public HashMap<String, Object> stringToMap(String input) {
        HashMap<String, Object> map = new HashMap<>();
        Scanner scanner = new Scanner(input);
        scanner.useDelimiter(",");

        while (scanner.hasNext()) {
            String[] keyValue = scanner.next().split("=");
            map.put(keyValue[0], keyValue[1]);
        }
        scanner.close();
        return map;
    }

    /**
     * Json 转 Map
     * @param json
     * @return
     */
    public Map<String,Object> jsonToMap(String json){
        Gson gson = new Gson();
        Type type = new TypeToken<Map<String, Object>>(){}.getType();
        Map<String, Object> map = gson.fromJson(json, type);
        System.out.println(map);
        return map;
    }

}
